/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
  <file> uls_log.c </file>
  <brief>
    A part of logging framework of ULS.
    This file is part of ULS, Unified Lexical Scheme.
  </brief>
  <author>
    Stanley J. Hong <babel96@gmail.com>, August 2011.
  </author>
*/

#include "common.h"
#include "uls_log.h"

static DECLARE_ULS_MUTEX(syserr_global_mtx);
static void *syslog;
static uls_lf_puts_t syslog_puts;
static uls_lf_csz_t mb_lbuf;

#define N_CHARS_LOGBUF 81

void initialize_syserr(void)
{
	uls_init_mutex(&syserr_global_mtx);

	syslog = (void *) stderr;
	syslog_puts = uls_lf_puts_file;

	uls_lf_csz_init(&mb_lbuf, NULL);
}

void finalize_syserr(void)
{
	uls_lf_csz_deinit(&mb_lbuf);
	uls_deinit_mutex(&syserr_global_mtx);
}

void err_puts(const uls_lf_char_t* msg)
{
	int len = strlen(msg);

	uls_lock_mutex(&syserr_global_mtx);
	syslog_puts(syslog, msg, len);
	uls_unlock_mutex(&syserr_global_mtx);
}

void err_log_puts(const uls_lf_char_t* mesg)
{
	uls_lock_mutex(&syserr_global_mtx);
	syslog_puts(syslog, mesg, strlen(mesg));
	syslog_puts(syslog, "\n", 1);
	uls_unlock_mutex(&syserr_global_mtx);
}

void err_panic_puts(const uls_lf_char_t* mesg)
{
	err_log_puts(mesg);
	exit(1);
}

void
err_vlog(const uls_lf_char_t* fmt, va_list args)
{
	uls_lock_mutex(&syserr_global_mtx);
	uls_lf_vxprintf_generic(NULL, syslog, syslog_puts, NULL, (uls_lf_char_t *) fmt, args);
	syslog_puts(syslog, "\n", 1);
	uls_unlock_mutex(&syserr_global_mtx);
}

void err_log(const uls_lf_char_t* fmt, ...)
{
	va_list	args;

	va_start(args, fmt);
	err_vlog(fmt, args);
	va_end(args);
}

void err_panic(const uls_lf_char_t* fmt, ...)
{
	va_list	args;

	va_start(args, fmt);
	err_vlog(fmt, args);
	va_end(args);

	exit(1);
}

void err_wputs(const uls_lf_word_t* wmsg)
{
	int len;

	uls_lock_mutex(&syserr_global_mtx);

	uls_lf_csz_reset(&mb_lbuf, NULL);
	len = uls_lf_zwprintf(&mb_lbuf, NULL, L"%ls", wmsg);
	syslog_puts(syslog, (uls_lf_char_t *) csz_text(&mb_lbuf.csz), len);

	uls_unlock_mutex(&syserr_global_mtx);
}

void
err_vwlog(const uls_lf_word_t* wfmt, va_list args)
{
	int len;

	uls_lock_mutex(&syserr_global_mtx);

	uls_lf_csz_reset(&mb_lbuf, NULL);
	len = uls_lf_vzwprintf(NULL, &mb_lbuf, NULL, (uls_lf_word_t *) wfmt, args);

	syslog_puts(syslog, (uls_lf_char_t *) csz_text(&mb_lbuf.csz), len);
	syslog_puts(syslog, "\n", 1);

	uls_unlock_mutex(&syserr_global_mtx);
}

void err_wlog(const uls_lf_word_t* wfmt, ...)
{
	va_list	args;
	
	va_start(args, wfmt);
	err_vwlog(wfmt, args);
	va_end(args);
}

void err_wpanic(const uls_lf_word_t* wfmt, ...)
{
	va_list	args;
	
	va_start(args, wfmt);
	err_vwlog(wfmt, args);
	va_end(args);

	exit(1);
}

void
uls_set_syserr(uls_lex_t* uls, void* data, uls_lf_puts_t proc)
{
	if (proc == NULL) {
		if (data == NULL) {
			proc = uls_lf_puts_null;
		} else {
			proc = uls_lf_puts_file;
		}
	}

	uls_lock_mutex(&syserr_global_mtx);
	uls->syslog = data;
	uls->syslog_puts = proc;
	uls_unlock_mutex(&syserr_global_mtx);
}

// <brief>
// A method that emits a message after formatting the string 'fmt' with 'args'
// </brief>
// <parm name="fmt">The format string, a template for printing</parm>
// <parm name="args">The list of args</parm>
// <return>none</return>
void
uls_vlog(uls_lex_t* uls, const uls_lf_char_t* fmt, va_list args)
{
	uls_lf_char_t numbuf[N_CHARS_LOGBUF];
	int len;

	if (uls == NULL) {
		err_vlog(fmt, args);
		return;
	}

	uls_lock_mutex(&syserr_global_mtx);

	len = uls_snprintf(numbuf, N_CHARS_LOGBUF, "[%s:%d] ",
		uls_get_tag(uls), uls_get_lineno(uls));
	uls->syslog_puts(uls->syslog, numbuf, len);

	uls_lf_vxprintf_generic(NULL, uls->syslog, uls->syslog_puts, uls, (uls_lf_char_t *) fmt, args);
	uls->syslog_puts(uls->syslog, "\n", 1);

	uls_unlock_mutex(&syserr_global_mtx);
}

// <brief>
// Logs a formatted message
// You can use %k %w to print the current token, its location.
// No need to append '\n' to the end of line 'fmt'
// </brief>
// <parm name="fmt">The template for message string</parm>
// <return>void</return>
void uls_log(uls_lex_t* uls, const uls_lf_char_t* fmt, ...)
{
	va_list	args;

	va_start(args, fmt);
	uls_vlog(uls, fmt, args);
	va_end(args);
}

// <brief>
// Logs a formatted message and the program will die.
// No need to append '\n' to the end of line 'fmt'
// </brief>
// <parm name="fmt">The template for message string</parm>
// <return>none</return>
void uls_panic(uls_lex_t* uls, const uls_lf_char_t* fmt, ...)
{
	va_list	args;

	va_start(args, fmt);
	uls_vlog(uls, fmt, args);
	va_end(args);

	exit(1);
}

// <brief>
// A method that emits a wide string message after formatting the string 'wfmt' with 'args'
// </brief>
// <parm name="wfmt">The format string, a template for printing</parm>
// <parm name="args">The list of args</parm>
// <return>none</return>
void
uls_vwlog(uls_lex_t* uls, const wchar_t* wfmt, va_list args)
{
	int len;

	if (uls == NULL) {
		err_vwlog(wfmt, args);
		return;
	}

	uls_lock_mutex(&syserr_global_mtx);

	uls_lf_csz_reset(&uls->mb_lbuf, NULL);
	len = uls_lf_zwprintf(&uls->mb_lbuf, uls, L"[%s:%d] ",
		uls_get_tag(uls), uls_get_lineno(uls));
	uls->syslog_puts(uls->syslog, (uls_lf_char_t *) csz_text(&uls->mb_lbuf.csz), len);

	uls_lf_csz_reset(&uls->mb_lbuf, NULL);
	len = uls_lf_vzwprintf(NULL, &uls->mb_lbuf, uls, (wchar_t *) wfmt, args);

	uls->syslog_puts(uls->syslog, (uls_lf_char_t *) csz_text(&uls->mb_lbuf.csz), len);
	uls->syslog_puts(uls->syslog, "\n", 1);

	uls_unlock_mutex(&syserr_global_mtx);
}

// <brief>
// An wide string version of the method 'log()'
// </brief>
// <parm name="loglvl">This message will be printed if 'loglvl' is set.</parm>
// <parm name="wfmt">The template for message string</parm>
// <return>none</return>
void uls_wlog(uls_lex_t* uls, const wchar_t* wfmt, ...)
{
	va_list	args;

	va_start(args, wfmt);
	uls_vwlog(uls, wfmt,args);
	va_end(args);
}

// <brief>
// An wide string version of the method 'panic()'
// </brief>
// <parm name="wfmt">The template for message string</parm>
// <return>none</return>
void uls_wpanic(uls_lex_t* uls, const wchar_t* wfmt, ...)
{
	va_list	args;

	va_start(args, wfmt);
	uls_vwlog(uls, wfmt,args);
	va_end(args);

	exit(1);
}
